package me.shurufa.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;

import me.shurufa.model.User;

/**
 * 本地持久化SharePerference
 *
 * @FileName: me.shurufa.utils.Settings.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-14 18:32
 */
public class Settings {
  public static final String PREF_NAME = "preferences";
  public static final String VERSION_KEY = "version_key";  // 用于存储新版本版本号
  public static final String IGNORE_UPDATE = "ignore_update"; // 忽略更新后 此版本不再提示
  private static final String TOKEN_KEY = "token";

  public static final String USER_KEY = "uid_";
  public static final String CURR_USER_KEY = "currUser";
  public static final String UIDS_KEY = "uids";

  public static SharedPreferences getPrefs() {
    int code = Context.MODE_MULTI_PROCESS;
    return Global.getContext().getSharedPreferences(PREF_NAME, code);
  }

  public static SharedPreferences.Editor getPrefsEditor() {
    return getPrefs().edit();
  }

  /**
   * 保存新版本号
   * @param newVersion
   */
  public static void setNewVersion(String newVersion) {
      SharedPreferences.Editor edit = getPrefsEditor();
      // 先判断本地存的最新版本是多少，用于忽略更新
      SharedPreferences prefs = getPrefs();
      String version = prefs.getString(VERSION_KEY, "");
      // 若新老版本不匹配，则将忽略更新重置
      if (!version.equals(newVersion)) {
          edit.putBoolean(IGNORE_UPDATE, false);
      }
      // 存新版本号
      edit.putString(VERSION_KEY, newVersion);
      edit.commit();
  }

  /**
   * 获得本地保存的最新版本号
   * @return
   */
  public static String getLocalNewVersion() {
      SharedPreferences prefs = getPrefs();
      String version = prefs.getString(VERSION_KEY, "");
      return version;
  }

  /**
   * 获得是否忽略该新版本的更新
   * @return
   */
  public static boolean getIgnoreUpdate() {
      SharedPreferences prefs = getPrefs();
      return prefs.getBoolean(IGNORE_UPDATE, false);
  }

  /**
   * 设置是否忽略本版本的更新
   * @param ignoreUpdate
   */
  public static void setIgnoreUpdate(boolean ignoreUpdate) {
      SharedPreferences.Editor edit = getPrefsEditor();
      edit.putBoolean(IGNORE_UPDATE, ignoreUpdate);
      edit.commit();
  }

  /**
   * 登录成功调用
   * 保存用户至sp
   * 
   * @param user
   */
  public static void saveUser(User user) {
    Gson gson = Global.getGson();
    String profile = gson.toJson(user);
    SharedPreferences prefs = getPrefs();
    String data = prefs.getString(UIDS_KEY, "");

    if (TextUtils.isEmpty(data)) {
      data = String.valueOf(user.id);
    } else {
      String[] uids = data.split(",");
      for (String uid : uids) {
        if (Long.parseLong(uid) == user.id) {
          return;
        }
      }
      data = data + "," + user.id;
    }
    SharedPreferences.Editor edit = getPrefsEditor();
    edit.putString(USER_KEY + user.id, profile);
    edit.putString(UIDS_KEY, data);
    edit.commit();
  }

  /**
   * @return 用户是否登录
   */
  public static boolean isLogin() {
    User currentUser = getCurrentUser();
    return currentUser != null;
  }

  /**
   * 删除用户 注销时调用
   * 
   * @param uidToDel
   */
  public static void delUser(String uidToDel) {
    SharedPreferences prefs = getPrefs();
    String[] uids = getUids();
    if (uids.length == 0)
      return;
    StringBuilder sb = new StringBuilder();
    for (String uid : uids) {
      if (uid.equals(uidToDel)) {
        continue;
      }
      if (sb.length() > 0)
        sb.append(',');
      sb.append(uid);
    }
    SharedPreferences.Editor edit = getPrefsEditor();
    edit.remove(USER_KEY + uidToDel);
    edit.putString(UIDS_KEY, sb.toString());
    edit.commit();
  }

  /**
   * 预留方法 用于多用户账号切换
   * 
   * @return
   */
  public static String[] getUids() {
    SharedPreferences prefs = getPrefs();
    String data = prefs.getString(UIDS_KEY, "");
    if (TextUtils.isEmpty(data))
      return new String[0];
    return data.split(",");
  }

  /**
   * 设置当前用户的uid
   * 
   * @param uid
   */
  public static void setCurrentUid(long uid) {
    SharedPreferences.Editor edit = getPrefsEditor();
    edit.putLong(CURR_USER_KEY, uid);
    edit.commit();
  }

  /**
   * 获取当前的user
   * 
   * @return
   */
  public static User getCurrentUser() {
    SharedPreferences prefs = getPrefs();
    long uid = prefs.getLong(CURR_USER_KEY, 0);
    if (uid == 0L) {
      return null;
    }
    return getUser(String.valueOf(uid));
  }

  /**
   * 预留方法
   * 获取user
   * 
   * @param uid
   * @return
   */
  public static User getUser(String uid) {
    SharedPreferences prefs = getPrefs();
    String profile = prefs.getString(USER_KEY + uid, "");
    Gson gson = Global.getGson();
    User user = gson.fromJson(profile, User.class);
    return user;
  }
}
